package com.eastfair.config.service.entityservice.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.config.entity.ConfigAttributeValue;
import com.eastfair.config.entity.ThemeColor;
import com.eastfair.config.service.ConfigAttributeValueService;
import com.eastfair.config.service.entityservice.ThemeColorService;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.enums.ModuleTypeEnum;
import com.eastfair.enums.TerminalTypeEnum;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.projectcore.dto.ConfigAttributeDTO;
import com.eastfair.projectcore.vo.ConfigAttributeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_ID;
import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_PARAMS;

/**
 * 主题色服务
 * Date:2021/8/7
 * Time:15:45
 *
 * @author zjn
 */
@Service
public class ThemeColorServiceImpl implements ThemeColorService {

    @Autowired
    private ProjectCoreServiceFeign projectCoreServiceFeign;
    @Autowired
    private ConfigAttributeValueService configAttributeValueService;

    @Override
    public List<ConfigAttributeVO> getAttributes() {
        ConfigAttributeDTO configAttributeDTO = new ConfigAttributeDTO();
        configAttributeDTO.setEntityClass(ThemeColor.class.getName());
        return projectCoreServiceFeign.listByEntityClass(configAttributeDTO).getData();
    }

    @Override
    public List<ThemeColor> getList(ThemeColor themeColor) {
        List<ThemeColor> themeColorList = configAttributeValueService.listEntityByClass(ThemeColor.class);
        if (themeColorList == null || themeColorList.isEmpty()) {
            return null;
        }
        if (Objects.isNull(themeColor)) {
            return themeColorList;
        }
        // 选择性过滤
        return themeColorList.stream().filter(s -> compare(s, themeColor)).collect(Collectors.toList());
    }

    @Override
    public R<ThemeColor> get(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }
        ThemeColor themeColor = ThemeColor.builder().flagId(flagId).build();
        List<ThemeColor> themeColorList = getList(themeColor);
        if (themeColorList != null && !themeColorList.isEmpty()) {
            return R.success(themeColorList.get(0));
        }
        return R.success(null);
    }

    @Override
    public R<ThemeColor> getByEntity(ThemeColor themeColor) {
        if (StringUtils.isBlank(themeColor.getModuleType(), themeColor.getTerminalType())) {
            return R.fail(ERROR_NULL_PARAMS);
        }

        ThemeColor param = ThemeColor.builder().moduleType(ModuleTypeEnum.get(themeColor.getModuleType()).name())
                .terminalType(TerminalTypeEnum.get(themeColor.getTerminalType()).getCode()).build();
        if (Objects.isNull(themeColor.getIsEnabled())) {
            param.setIsEnabled(BusinessConstant.ENABLE_YES);
        } else {
            param.setIsEnabled(themeColor.getIsEnabled());
        }

        List<ThemeColor> themeColorList = getList(themeColor);
        if (themeColorList != null && !themeColorList.isEmpty()) {
            return R.success(themeColorList.get(0));
        }
        return R.success(null);

    }

    @Override
    public R<Boolean> saveEntity(ThemeColor themeColor) {
        if (StringUtils.isBlank(themeColor.getTerminalType(), themeColor.getModuleType(), themeColor.getName(), themeColor.getPictureDefault(), themeColor.getPictureIphoneX())) {
            return R.fail(ERROR_NULL_PARAMS);
        }
        if (Objects.isNull(themeColor.getIsEnabled())) {
            themeColor.setIsEnabled(BusinessConstant.ENABLE_YES);
        }

        return R.success(configAttributeValueService.saveEntity(themeColor));
    }

    @Override
    public R<Boolean> deleteEntity(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }

        UpdateWrapper<ConfigAttributeValue> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ConfigAttributeValue::getProjectId, ContextUtil.getProjectId())
                .eq(ConfigAttributeValue::getFlagId, flagId).set(ConfigAttributeValue::getIsDelete, BusinessConstant.DELETE_YES);
        return R.success(configAttributeValueService.update(updateWrapper));
    }

    @Override
    public R<Boolean> updateEntity(ThemeColor themeColor) {
        if (Objects.isNull(themeColor.getFlagId())) {
            return R.fail(ERROR_NULL_ID);
        }
        if (StringUtils.isBlank(themeColor.getTerminalType()) && StringUtils.isBlank(themeColor.getModuleType())
                && StringUtils.isBlank(themeColor.getName()) && StringUtils.isBlank(themeColor.getPictureDefault())
                && StringUtils.isBlank(themeColor.getPictureIphoneX()) && Objects.isNull(themeColor.getIsEnabled())
        ) {
            return R.fail(ERROR_NULL_PARAMS);
        }

        return R.success(configAttributeValueService.updateEntity(themeColor));
    }

    /**
     * 主题色过滤器
     *
     * @param target   目标值
     * @param standard 过滤条件
     * @return
     */
    private Boolean compare(ThemeColor target, ThemeColor standard) {
        if (Objects.isNull(target) || Objects.isNull(standard)) {
            return true;
        }

        if (!Objects.isNull(standard.getFlagId())) {
            if (!standard.getFlagId().equals(target.getFlagId())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getName())) {
            if (StringUtils.isBlank(target.getName()) || !target.getName().contains(standard.getName())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getModuleType())) {
            if (!standard.getModuleType().equals(target.getModuleType())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getTerminalType())) {
            if (!standard.getTerminalType().equals(target.getTerminalType())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getColorStart())) {
            if (!standard.getColorStart().equals(target.getColorStart())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getColorEnd())) {
            if (!standard.getColorEnd().equals(target.getColorEnd())) {
                return false;
            }
        }
        if (!Objects.isNull(standard.getIsEnabled())) {
            if (!standard.getIsEnabled().equals(target.getIsEnabled())) {
                return false;
            }
        }
        return true;
    }
}